import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:helloworld/widget/local_nav.dart';
import 'package:helloworld/widget/grid_nav.dart';
import '../mock/home_page.dart';

const APPBAR_SCROLL_MAX = 100;

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _imageList = [
    'https://i0.hdslb.com/bfs/archive/2c9c0834b94b0d406f6b2bb854f611e2af5b5ef3.jpg@880w_388h_1c_95q',
    'https://i0.hdslb.com/bfs/archive/b62596a262170d64c3e5b8d233a8eb35ae70b3a4.png@880w_388h_1c_95q',
    'https://i0.hdslb.com/bfs/sycp/creative_img/202004/e90c8f6e91b995c772c9dd528c984606.jpg@880w_388h_1c_95q'
  ];
  double appBarAlpha = 0;
  _onScroll(offset) {
    double alpha = offset / APPBAR_SCROLL_MAX;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: <Widget>[
        //移除手机上的顶部状态栏
        MediaQuery.removePadding(
            removeTop: true,
            context: context,
            //监听滚动
            child: NotificationListener(
              onNotification: (scrollNotification) {
                //滚动的是第一个子元素  listView时才触发
                if (scrollNotification is ScrollUpdateNotification &&
                    scrollNotification.depth == 0) {
                  _onScroll(scrollNotification.metrics.pixels);
                }
              },
              child: ListView(children: <Widget>[
                Container(
                  height: 160,
                  child: Swiper(
                    // itemCount: home_data['bannerList'].length,
                    itemCount: _imageList.length,
                    autoplay: true,
                    itemBuilder: (context, index) {
                      return Image.network(_imageList[index],
                          // home_data['bannerList'][index]['icon'],
                          fit: BoxFit.cover);
                    },
                    pagination: SwiperPagination(),
                  ),
                ),
                LocalNav(localNavList: home_data['localNavList']),
                GridNav(
                  gridData: home_data['gridNav'],
                )
              ]),
            )),
        Opacity(
            opacity: appBarAlpha,
            child: Container(
              height: 80,
              decoration: BoxDecoration(color: Colors.white),
              child: Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Text('首页'),
                ),
              ),
            ))
      ],
    ));
  }
}
